package com.szqhyxj.youxiangju.biz.merchantHome.shop.order;

import com.szqhyxj.youxiangju.biz.merchantHome.shop.OrderList;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class OrderPresenter extends AbBasePresenter implements OrderContract.Presenter {

    private final OrderContract.View view;

    public OrderPresenter(OrderContract.View view){
        this.view = view;
    }


    @Override
    public void init(){
        addDisposables(OrderModel.getOrderList()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
        .subscribe(new Consumer<BaseResult<OrderList>>(){
            @Override
            public void accept(BaseResult<OrderList> orderListBaseResult) throws Exception{
                view.dismissLoading();
                if(orderListBaseResult.businessLogicSuccess()){
                    if(orderListBaseResult.getData().getRows().size()>0){
                        view.showOrderList(orderListBaseResult.getData().getRows());
                    }else {
                        view.showNoOrder();
                    }
                }else {
                    view.showToast(orderListBaseResult.getMessage());
                    view.showNoOrder();
                }
            }
        }, new Consumer<Throwable>(){
            @Override
            public void accept(Throwable throwable) throws Exception{
                view.showToast(throwable.getMessage());
                view.dismissLoading();
                view.showNoOrder();
            }
        }));
    }

    @Override
    public void nowConfirm(final int i, String orderId){
        addDisposables(OrderModel.confirmReceipt(orderId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.showConfirmOrder(i);
                        }else{
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }
}